<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Business\Admin\Account\AccountManagement;
use App\Http\Controllers\Business\Admin\Auth\AdminAuth;
use App\Http\Controllers\Business\Admin\Shop\ShopManagement;
use App\Http\Controllers\Business\App\MyCenter\MyCenter;
use App\Http\Controllers\Business\App\Today\Today;
use App\Http\Controllers\Business\App\Wallet\RechargeNotifyController;
use App\Http\Controllers\Business\App\Wallet\Wallet;
use App\Http\Controllers\Business\Shop\V1\PublicBusiness;
use App\Http\Requests;
use App\Models\AdminRole;
use App\Models\AdminUser;
use App\Models\AllInPay;
use App\Models\AllInPayTooles;
use App\Models\ArrayAndXml;
use App\Models\BusinessArea;
use App\Models\City;
use App\Models\Comment;
use App\Models\District;
use App\Models\FirstClass;
use App\Models\Log;
use App\Models\Order;
use App\Models\Oss;
use App\Models\Province;
use App\Models\Recharge;
use App\Models\SecondClass;
use App\Models\Shop;
use App\Models\ShopContract;
use App\Models\ShopEvent;
use App\Models\ShopSalesman;
use App\Models\ShopSalesmanDetail;
use App\Models\ShopSalesmanWithdrawal;
use App\Models\ShopUser;
use App\Models\SmsMessage;
use App\Models\User;
use App\Utils\DeviceTool;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use TopClient;
use AlibabaAliqinFcSmsNumSendRequest;
use Sts\Request\V20150401 as Sts;

class HomeController extends Controller
{
//    protected $guard = 'adminweb';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    function formatBizQueryParaMap($paraMap, $urlencode)
    {
        $buff = "";
        ksort($paraMap);
        foreach ($paraMap as $k => $v)
        {
            if($urlencode)
            {
                $v = urlencode($v);
            }
            //$buff .= strtolower($k) . "=" . $v . "&";
            $buff .= $k . "=" . $v . "&";
        }
        $reqPar='';
        if (strlen($buff) > 0)
        {
            $reqPar = substr($buff, 0, strlen($buff)-1);
        }
        return $reqPar;
    }

    public function index1(Request $request)
    {
        set_time_limit(0);
        $time = $request->get('time');
        if(!$time)
        {exit;}
        $b = rand(1500,3500);
        for($i = 0;$i < $b;$i++)
        {
            try
            {
                $rand1 = rand(0,9);
                if($rand1 < 1)
                {
                    $timerand = strtotime($time) + rand(28800,36000);
                }
                elseif ($rand1 > 8)
                {
                    $timerand = strtotime($time) + rand(72000,79200);
                }
                else
                {
                    $timerand = strtotime($time) + rand(36000,72000);
                }
                $userid = rand(1,192623);
                $mobile = User::where('id',$userid)->pluck('mobile')->first();
                if($mobile) {
                    $mobile = substr($mobile,8,10);
                    $rand = '';
                    for($i1=0;$i1<4;$i1++){
                        $rand .= rand(0,9);
                    }
                    $ordernumber='1' . date('ymdHis',$timerand) . $rand.$mobile;
                }
                $money = rand(1,50) * 10;
                $recharge = new Recharge;
                $recharge->userid = $userid;
                $recharge->money = $money;
                $recharge->status = '1';
                $recharge->path = '支付宝';
                $recharge->rechargenumber = $ordernumber;
                $recharge->doublemoney = $money * 2;
                $recharge->doubletime = date('Y-m-d H:i:s',$timerand);
                $recharge->created_at = date('Y-m-d H:i:s',$timerand);
                $recharge->save();
                User::find($userid)->increment('balance',$money*2);
                User::find($userid)->increment('point',$money);
            }
            catch (\Exception $e)
            {
                continue;
            }
        }
        $b = rand(2500,4500);
        for($i = 0;$i < $b;$i++)
        {
            try
            {
                $rand1 = rand(0,9);
                if($rand1 < 1)
                {
                    $timerand = strtotime($time) + rand(28800,36000);
                }
                elseif ($rand1 > 8)
                {
                    $timerand = strtotime($time) + rand(72000,79200);
                }
                else
                {
                    $timerand = strtotime($time) + rand(36000,72000);
                }
                $userid = rand(1,192623);
                $user = User::find($userid);
                $shop = Shop::find(rand(1,922));
                $mobile = substr($user['mobile'],7,10);
                $rand = '';
                for($i1=0;$i1<4;$i1++) {
                    $rand .= rand(0,9);
                }
                $ordernumber=date('ymdHis',$timerand).$rand.$mobile;
                $money=rand(10,300);
                $order = new Order();
                $order->userid = $userid;
                $order->nickname = $user['nickname'];
                $order->shopid = $shop['id'];
                $order->shopname = $shop['name'];
                $order->shoplogo = $shop['logo'];
                $order->ordernumber = $ordernumber;
                $order->orderamount = $money;
                $order->actualamount = $money;
                $order->created_at = date('Y-m-d H:i:s',$timerand);
                $order->save();
            }
            catch (\Exception $e)
            {
                continue;
            }
        }
        $mobile = ["1300620","1300621","1300720","1300721","1300722","1300723","1300724","1300728","1301072","1302620","1302621","1302720","1302721","1302722","1302723","1302724","1302728","1303620","1303621","1303720","1303721","1303722","1303723","1303724","1303728","1304490","1304491","1304790","1304791","1306410","1306411","1306412","1306413","1306414","1306510","1306511","1306512","1306513","1306514","1306515","1306516","1306517","1306518","1306519","1307790","1307791","1307792","1307793","1307794","1307795","1307796","1307797","1307798","1307799","1309720","1309721","1309728","1311780","1311781","1311791","1312521","1313380","1313381","1313382","1313383","1313384","1313391","1315580","1315581","1315582","1315583","1317086","1317087","1317088","1317091","1317777","1317778","1317780","1317781","1317782","1317783","1317784","1317785","1317786","1317787","1317788","1317789","1317791","1318456","1318457","1318458","1318459","1319791","1320700","1320708","1320709","1320791","1321700","1321791","1323700","1323704","1323708","1323751","1323752","1323764","1323791","1324700","1324708","1324709","1324770","1324771","1324776","1324778","1324780","1324781","1324784","1324786","1324791","1325700","1325708","1325709","1325791","1326300","1326309","1326391","1330700","1330708","1330709","1330791","1331700","1331705","1331791","1331940","1331941","1331942","1331943","1332001","1332011","1333005","1333006","1333007","1333008","1333009","1333010","1333011","1333012","1334001","1334011","1336161","1336162","1336163","1336164","1336165","1336171","1336700","1336708","1336709","1336791","1338700","1338708","1338709","1338791","1339700","1339708","1339709","1339791","1340700","1340708","1340709","1340791","1342668","1343700","1343708","1343709","1343790","1343791","1347910","1347911","1347912","1347913","1347914","1347915","1347916","1347917","1347918","1347919","1350700","1350708","1350709","1350791","1351700","1351708","1351709","1351791","1357600","1357601","1357602","1357603","1357604","1357605","1357606","1357607","1357608","1357609","1357610","1357611","1357612","1357613","1357614","1357625","1357626","1357627","1357628","1357629","1357690","1357691","1357692","1357693","1357694","1357695","1357696","1357697","1357698","1357699","1360700","1360704","1360706","1360708","1360709","1360791","1361700","1361708","1361709","1361791","1362700","1362708","1362709","1362791","1364700","1364708","1364709","1364791","1365700","1365708","1365709","1365791","1366700","1366708","1366709","1366791","1367220","1367221","1367222","1367223","1367224","1367700","1367708","1367709","1367791","1368481","1368700","1368708","1368709","1368791","1369488","1369700","1369708","1369709","1369791","1369800","1369809","1369841","1369950","1369951","1369952","1369953","1369954","1369955","1369956","1370708","1370709","1370791","1373290","1373291","1373292","1373293","1373294","1373295","1373296","1373297","1373298","1373299","1374070","1374079","1374270","1374279","1375560","1375561","1375562","1375563","1375564","1375565","1375566","1375567","1375568","1375569","1375575","1375576","1375577","1375578","1375579","1376700","1376701","1376702","1376703","1376704","1376705","1376706","1376707","1376708","1376709","1376710","1376711","1376712","1376713","1376714","1376715","1376716","1376717","1376718","1376719","1376740","1376741","1376742","1376743","1376744","1376745","1376746","1376747","1376748","1376749","1376795","1376796","1376797","1376798","1376799","1380350","1380351","1380352","1380353","1380354","1380700","1380703","1380704","1380705","1380706","1380708","1380709","1380791","1387005","1387006","1387007","1387008","1387009","1387060","1387061","1387062","1387063","1387064","1387065","1387066","1387067","1387068","1387069","1387080","1387081","1387082","1387083","1387084","1387085","1387086","1387087","1387088","1387089","1387090","1387091","1387092","1387093","1387094","1387095","1387096","1387097","1387098","1387099","1387910","1387911","1387912","1387913","1387914","1387915","1387916","1387917","1387918","1387919","1390070","1390079","1390700","1390708","1390709","1390791","1397000","1397001","1397002","1397003","1397004","1397005","1397006","1397007","1397008","1397009","1397080","1397081","1397082","1397083","1397084","1397085","1397086","1397087","1397088","1397089","1397090","1397091","1397092","1397093","1397094","1397095","1397096","1397097","1397098","1397099","1397910","1397911","1397912","1397913","1397914","1397915","1397916","1397917","1397918","1397919","1500700","1500791","1507000","1507001","1507002","1507003","1507004","1507005","1507006","1507007","1507008","1507009","1507040","1507041","1507080","1507081","1507082","1507083","1507084","1507085","1507086","1507087","1507088","1507089","1507090","1507091","1507092","1507093","1507094","1507095","1507096","1507097","1507098","1507099","1507900","1507901","1507902","1507903","1507904","1507905","1507906","1507907","1507908","1507909","1507910","1507911","1507912","1507913","1507914","1507915","1507916","1507917","1507918","1507919","1508350","1508351","1508352","1508353","1508354","1508355","1508380","1508381","1508382","1508383","1508384","1510791","1517000","1517001","1517002","1517003","1517004","1517005","1517006","1517007","1517008","1517009","1517020","1517021","1517022","1517023","1517024","1517040","1517041","1517042","1517043","1517044","1517045","1517046","1517047","1517048","1517049","1517910","1517911","1517912","1517913","1517914","1517915","1517916","1517917","1517918","1517919","1518010","1518011","1518012","1518013","1518014","1518015","1518016","1518017","1518018","1518019","1518040","1518041","1518042","1518043","1518044","1518045","1518046","1518047","1518048","1518049","1527097","1527094","1527087","1527095","1530700","1530791","1533775","1534791","1550791","1557910","1557911","1557912","1557913","1557914","1557915","1557916","1557917","1557918","1560791","1567910","1567911","1567912","1567913","1567914","1567915","1567916","1567917","1567918","1567919","1570791","1571791","1572763","1572764","1572765","1572767","1572767","1572768","1571709","1580700","1580791","1587000","1587001","1587002","1587003","1587060","1587061","1587062","1587063","1587064","1587065","1587066","1587067","1587068","1587069","1587900","1587901","1587902","1587903","1587904","1587905","1587906","1587907","1587908","1587909","1587910","1587911","1587912","1587913","1587914","1587915","1587916","1587917","1587918","1587919","1590700","1590708","1590709","1590791","1597040","1597041","1597042","1597043","1597044","1597045","1597046","1597047","1597048","1597049","1597060","1597061","1597062","1597063","1597064","1597065","1597066","1597067","1597068","1597069","1597900","1597901","1597902","1597903","1597904","1597905","1597906","1597907","1597908","1597909","1597910","1597911","1597912","1597913","1597914","1597915","1597916","1597917","1597918","1597919","1880708","1887007","1897090","1897914"];
        for($i = 0;$i < rand(500,2000);$i++)
        {
            try
            {
                $rand1 = rand(0,9);
                if($rand1 < 1)
                {
                    $timerand = strtotime($time) + rand(28800,36000);
                }
                elseif ($rand1 > 8)
                {
                    $timerand = strtotime($time) + rand(72000,79200);
                }
                else
                {
                    $timerand = strtotime($time) + rand(36000,72000);
                }
                $user = new User();
                $user->mobile = $mobile[rand(0,count($mobile) - 1)] . rand(1000,9999);
                $user->nickname = 'fbw_' . $timerand;
                $user->avatar = 'http://img.fbw-china.com/images/default_avatar.png';
                $user->created_at = date('Y-m-d H:i:s',$timerand);
                $user->save();
            }
            catch (\Exception $e)
            {
                continue;
            }
        }
        exit;
        set_time_limit(0);
        $ip = $request->get('ip');
        $shopid = $request->get('shopid');
        $user = User::where('ip',$ip)->where('shopid',$shopid)->whereNull('info')->get();
        $i = 0;
        foreach ($user as $v)
        {
            $shopgetmoney = ShopSalesmanDetail::where('mobile',$v['mobile'])->pluck('getmoney')->first();
            if($shopgetmoney) {
                if ($shopgetmoney == 5) {
                    ShopSalesmanDetail::where('mobile', $v['mobile'])->update(['getmoney' => '0']);
                    ShopSalesman::where('shopid', $v['shopid'])->decrement('commonregisternum', 1);
                    ShopSalesman::where('shopid', $v['shopid'])->decrement('balance', $shopgetmoney);
                } elseif ($shopgetmoney == 10) {
                    ShopSalesmanDetail::where('mobile', $v['mobile'])->update(['getmoney' => '0']);
                    ShopSalesman::where('shopid', $v['shopid'])->decrement('partnerregisternum', 1);
                    ShopSalesman::where('shopid', $v['shopid'])->decrement('balance', $shopgetmoney);
                }
                User::where('id',$v['id'])->update(['info' => '虚假地推数据']);
                $i++;
            }
        }
        echo $i;
    }
    

    public function index(Request $request)
    {
        $money=11661*0.01;
        $aaa = new RechargeNotifyController();
        $userId='1';
        $userdata = json_decode('{"userid":"1","money":"110","actualmoney":116.6,"fee":6.6,"apiversion":"v2"}',true);
        $path = '测试';
        $ordernumber = '21703012047379470343';
        $payshopnumber = '4000112001201703011845977385';
        if(bccomp($money,$userdata['actualmoney'],2))
        {
            echo '123123';
        }
        else
        {
            echo '345345';
            echo $money . '<br>';
            echo $userdata['actualmoney'];
        }
        echo bccomp($money,$userdata['actualmoney'],2);
        exit;
        $aaa->addRechargeByV2Version($userdata['userid'], $userdata['money'], $userdata['fee'], $userdata['actualmoney'], $path, $ordernumber, $payshopnumber);
        exit;
        $aaa = new AllInPayTooles();
        echo json_encode($aaa->getPayStatus('11451484704632314916'));
        exit;
        $data = ShopSalesman::where('type','partner')->where('status','1')->get();
        foreach ($data as $v)
        {
            $aaa = new ShopSalesmanWithdrawal();
        $aaa->shopid=$v['shopid'];
        $aaa->accountamount = ShopSalesmanDetail::where('created_at','<','2017-01-01 00:00:00')->where('shopid',$v['shopid'])->sum('getmoney');
        $aaa->accountmonth = '2017-01-01';
        $aaa->status = 0;
        $aaa->save();
    }
        exit;
//        $aaa = '123iPad123';
//        if(!strstr($aaa,'iPhone') && !strstr($aaa,'iPad') && !strstr($aaa,'iPod'))
//        {
//            echo '123';
//        }
//        exit;
//        $user = User::where('ip','210.35.193.250')->where('shopid','967')->where('created_at','<','2017-01-08 00:00:00')
//            ->whereNotNull('info')->get();
//        $i = 0;
//        foreach ($user as $v)
//        {
//                    ShopSalesmanDetail::where('mobile', $v['mobile'])->update(['getmoney' => '10']);
//                    ShopSalesman::where('shopid', $v['shopid'])->increment('partnerregisternum', 1);
//                    ShopSalesman::where('shopid', $v['shopid'])->increment('balance', 10);
//                User::where('id',$v['id'])->update(['info' => '']);
//                $i++;
//        }
//        echo $i;
//        exit;
        set_time_limit(0);
        $ip = $request->get('ip');
        $shopid = $request->get('shopid');
//        $user = User::where('ip',$ip)->where('shopid',$shopid)->whereNull('info')->get();
        $user = User::where('registersource','AppStore')->where('shopid',$shopid)->where('phonetype','not like','%iPhone%')
            ->where('created_at','>','2017-01-01')
            ->whereNull('info')->get();
        $i = 0;
        foreach ($user as $v)
        {
            $shopgetmoney = ShopSalesmanDetail::where('mobile',$v['mobile'])->pluck('getmoney')->first();
            if($shopgetmoney) {
                if ($shopgetmoney == 5) {
                    ShopSalesmanDetail::where('mobile', $v['mobile'])->update(['getmoney' => '0']);
                    ShopSalesman::where('shopid', $v['shopid'])->decrement('commonregisternum', 1);
                    ShopSalesman::where('shopid', $v['shopid'])->decrement('balance', $shopgetmoney);
                } elseif ($shopgetmoney == 10) {
                    ShopSalesmanDetail::where('mobile', $v['mobile'])->update(['getmoney' => '0']);
                    ShopSalesman::where('shopid', $v['shopid'])->decrement('partnerregisternum', 1);
                    ShopSalesman::where('shopid', $v['shopid'])->decrement('balance', $shopgetmoney);
                }
                User::where('id',$v['id'])->update(['info' => '虚假地推数据']);
                $i++;
            }
        }
        echo $i;
        exit;
        $user = User::where('id','1')->get();
        $user = User::where('id','2')->get();
        $user = User::where('id','3')->get();
        $log = new Log();
        $log->logs = '1';
        $log->save();
        Log::find($log->id)->increment('logs',5);
        Log::find($log->id)->increment('logs',5);
        echo $user;
        exit;
        $user = User::where('shopid','<>','0')->where('ip','<>','')->whereNull('info')->get();
        foreach ($user as $v)
        {
            $city = DeviceTool::getCityByIp($v['ip']);
            if($city['data']['city']) {
                User::where('id', $v['id'])->update(['info' => $city['data']['city']]);
            }
        }
        exit;
    }

    function getRandChar($length){
        $str = null;
        $strPol = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
        $max = strlen($strPol)-1;

        for($i=0;$i<$length;$i++){
            $str.=$strPol[rand(0,$max)];
        }

        return $str;
    }


}
